package com.bmft.grid_view11;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.bmft.MainActivity;
import com.bmft.R;

public class GridViewActivity11 extends AppCompatActivity {

    private GridView gv_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view11);
        gv_1= findViewById(R.id.gv_1);
        //使用adapter填充
        gv_1.setAdapter(new MyGridViewAdapter(GridViewActivity11.this, 10));
        //设计点击试事件
        gv_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(GridViewActivity11.this, "click pos:" + position, Toast.LENGTH_SHORT).show();
            }
        });
        //设计长按事件
        gv_1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(GridViewActivity11.this, "long click  pos:" + position, Toast.LENGTH_SHORT).show();
                //结束事件，不继续显示单击事件，建议使用。
                return true;
            }
        });
    }

    public void backToMain(View view) {
        Intent intent = new Intent(GridViewActivity11.this, MainActivity.class);
        startActivity(intent);
    }
}